/*******************************************************************
 *   > File Name: uart.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: Tue 04 Feb 2020 01:13:14 PM CST
 ******************************************************************/
#define GPA0CON     0xE0200000
#define ULCON0      0xE2900000
#define UCON0       0xE2900004
#define UFCON0      0xE2900008
#define UMCON0      0xE290000C
#define UTRSTAT0    0xE2900010
#define UERSTAT0    0xE2900014
#define UFSTAT0     0xE2900018
#define UMSTAT0     0xE290001C
#define UTXH0       0xE2900020
#define URXH0       0xE2900024
#define UBRDIV0     0xE2900028
#define UDIVSLOT0   0xE290002C
#define UINTP0      0xE2900030
#define UINTM0      0xE2900038

#define rGPA0CON    (*(volatile unsigned *)GPA0CON)
#define rULCON0     (*(volatile unsigned *)ULCON0)
#define rUCON0      (*(volatile unsigned *)UCON0)
#define rUFCON0     (*(volatile unsigned *)UFCON0)
#define rUMCON0     (*(volatile unsigned *)UMCON0)
#define rUBRDIV0    (*(volatile unsigned *)UBRDIV0)
#define rUDIVSLOT0  (*(volatile unsigned *)UDIVSLOT0)
#define rUTRSTAT0   (*(volatile unsigned *)UTRSTAT0)
#define rUTXH0      (*(volatile unsigned *)UTXH0)
#define rURXH0      (*(volatile unsigned *)URXH0)

void uart_init(void)
{
    rGPA0CON &= ~(0xff<<0);//clear bit[7:0]
    rGPA0CON |= 0x00000022;//0b0010

    rULCON0 = 0x03;
    rUCON0 = 0x5;
    rUMCON0 = 0x0;
    rUFCON0 = 0x0;

    rUBRDIV0 = 35;
    rUDIVSLOT0 = 0x0888;
}

void uart_putc(char c)
{
    while(!(rUTRSTAT0 & (1 << 1)));
    rUTXH0 = c;
}

char uart_getc(void)
{
    while(!(rUTRSTAT0 & (1 << 0)));
    return (rURXH0 & 0x0f);
}
